//
//  KKFileManager.m
//  GCDDemo
//
//  Created by 程强 on 16/7/5.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "KKFileManager.h"

@implementation KKFileManager
+ (NSString *)documentPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    return docDir;
}
+ (NSString *)createFileWithName:(NSString *)fileName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //获取document路径,括号中属性为当前应用程序独享
    NSString *documentDirectory = [self  documentPath];
    //定义记录文件全名以及路径的字符串filePath
    
    NSString *filePath = [documentDirectory stringByAppendingPathComponent:fileName];
    
    //查找文件，如果不存在，就创建一个文件
    
    if (![fileManager fileExistsAtPath:filePath]) {
        BOOL success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
        if (success) {
            return filePath;
        }else
            NSAssert(success, @"创建文件失败");
    }
    return filePath;
}
#pragma mark --- 沙盒写文件
+ (BOOL)saveObject:(id)object withPath:(NSString *)path
{
    if (!object) {
        NSString *errorLog = [NSString stringWithFormat:@"写入文件为空！！！Class Name：%@ func：%s line：%d",NSStringFromClass([self class]),__func__,__LINE__];
        NSAssert(object, errorLog);
    }
    BOOL success ;
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        if ([object isKindOfClass:[NSArray class]]) {
            success = [(NSArray *)object writeToFile:path atomically:YES];
        }else
            success = [object writeToFile:path options:NSDataWritingAtomic error:nil];
        NSLog(@"写入沙盒的文件路径是%@",path);
    }
    return success;
}
+ (BOOL)saveObject:(id)object withName:(NSString *)name
{
    if (!object) {
        NSString *errorLog = [NSString stringWithFormat:@"写入文件为空！！！Class Name：%@ func：%s line：%d",NSStringFromClass([self class]),__func__,__LINE__];
        NSAssert(object, errorLog);
    }
    BOOL success ;
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *documentDirectory = [self  documentPath];
    NSString *filePath = [documentDirectory stringByAppendingPathComponent:name];
    if ([manager fileExistsAtPath:filePath]) {
        if ([object isKindOfClass:[NSArray class]]) {
            success = [(NSArray *)object writeToFile:filePath atomically:YES];
        }else
            success = [object writeToFile:filePath options:NSDataWritingAtomic error:nil];
        NSLog(@"写入沙盒的文件路径是%@",filePath);
    }
    return success;
}
#pragma mark --- NSUserDefaults存储
+ (BOOL)setUserDefaultObject:(id)object forKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults]setObject:object forKey:key];
    BOOL success = [[NSUserDefaults standardUserDefaults]synchronize];
    return success;
}
+ (id)userDefaultObjectForKey:(NSString *)key
{
    id object = [[NSUserDefaults standardUserDefaults]objectForKey:key];
    return object;
}
#pragma mark --- 归档
/**
 *  归档《字符串、数字、NSArray与NSDictionary》
 */
+ (BOOL)arichiverObject:(id)object toFile:(NSString *)path
{
    BOOL success;
    success = [NSKeyedArchiver archiveRootObject:object toFile:path];
    return success;
}
+ (id)unarchiverObjectWithFile:(NSString *)path
{
    id object = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return object;
}
@end
